home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************
- * *
- * Life 0.4 © Copyright 1989 by Garth Dickie. *
- * *
- * You can do anything non-commercial with this you like. This includes using the source and all *
- * that stuff. If you want to make money off of something that uses this, check with me first. *
- * *
- * I can be reached at 'garth@caen.engin.umich.edu' or 'Garth_Dickie@ub.cc.umich.edu' *
- * *
- *****************************************************************/
-
- #include "main.h"
- #include "menus.h"
- #include "cursors.h"
- #include "patch.h"
-
- extern long doGrowWindow();
- extern pascal doErrSound();
-
- int isBackground = 0;
-
- static int inBack = 0;
-
- main()
- {
- int waitNext;
-
- MoreMasters();
- MaxApplZone();
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NULL );
- ErrorSound( doErrSound );
- InitCursor();
-
- initDialogs();
- getMenus();
- initScrap();
-
- waitNext = implemented( WNETrapNum );
- installFileTraps();
- doFile( fmNew );
- while( mainEvent( waitNext ));
- removeFileTraps();
- }
-
- int mainEvent( waitNext )
- int waitNext;
- {
- static RgnHandle mouseGood = NULL;
- EventRecord theEvent;
- int result = -1, wasEvt;
- long toWait;
-
- if( !mouseGood ) mouseGood = NewRgn();
- if( !inBack ) updateMouse( mouseGood );
- if( waitNext ) {
- toWait = isBackground ? 0 : waitTime;
- wasEvt = WaitNextEvent( everyEvent, &theEvent, toWait, inBack ? NULL : mouseGood );
- } else {
- SystemTask();
- wasEvt = GetNextEvent( everyEvent, &theEvent );
- }
- if( wasEvt ) result = doEvent( &theEvent );
- else doBackground( inBack ? lotsElseToDo : nothingElseToDo, noGray, NULL );
- if( !result ) DisposeRgn( mouseGood );
- return( result );
- }
-
- int doEvent( theEvent )
- EventRecord *theEvent;
- {
- WindowPtr whichWindow;
- char theChar;
- int result = -1;
-
- switch (theEvent->what) {
- case mouseDown:
- return( doMouse( theEvent ));
- break;
- case keyDown:
- case autoKey:
- theChar = theEvent->message & charCodeMask;
- if ( theEvent->modifiers & cmdKey ) {
- updateMenus();
- result = doCommand( MenuKey( theChar ));
- } else if( theChar == DEL )
- doEdit( edClear );
- break;
- case updateEvt:
- doUpdate( theEvent->message );
- break;
- case activateEvt:
- if( theEvent->modifiers & activeFlag )
- doActivateEvt( theEvent->message );
- else doDeactivateEvt( theEvent->message );
- break;
- case app4Evt:
- doApp4Evt( theEvent );
- break;
- default: ;
- }
- return( result );
- }
-
- int doMouse( theEvent )
- EventRecord *theEvent;
- {
- Rect sizeRect;
- WindowPtr whichWindow;
- long grown, command;
- int result = -1;
-
- switch ( FindWindow( theEvent->where, &whichWindow ))
- {
- case inGoAway:
- if( doTrackGoAway( whichWindow, theEvent->where ))
- doFile( fmClose );
- break;
- case inMenuBar:
- updateMenus();
- installMenuTrap();
- command = MenuSelect( theEvent->where );
- removeMenuTrap();
- result = doCommand( command );
- break;
- case inDrag:
- doDragWindow( whichWindow, theEvent->where, NULL );
- if((( WindowPeek )whichWindow )->windowKind >= userKind )
- doShift( whichWindow );
- break;
- case inGrow:
- setSizeRect( &sizeRect, whichWindow );
- grown = doGrowWindow( whichWindow, theEvent->where, &sizeRect );
- if( grown ) doSizeWindow( whichWindow, LoWord( grown ), HiWord( grown ));
- break;
- case inContent:
- if (FrontWindow() == whichWindow) doContent( whichWindow, theEvent );
- else doChangeTo( whichWindow );
- break;
- case inSysWindow:
- doSysWindow( theEvent, whichWindow );
- break;
- case inDesk:
- default: ;
- }
- return( result );
- }
-
- doApp4Evt( theEvent )
- EventRecord *theEvent;
- {
- int what;
-
- what = HiWord( theEvent->message ) / 256;
- switch( what )
- {
- case srEvt:
- if( theEvent->message & resumeFlag )
- doResume( theEvent );
- else doSuspend( theEvent );
- break;
- default: ;
- }
- }
-
- /*******************************************************
- * *
- * *
- *******************************************************/
-
- WindowPtr frontUserWindow()
- {
- WindowPeek theWindow;
-
- theWindow = (WindowPeek)FrontWindow();
- while( theWindow && theWindow->windowKind < userKind )
- theWindow = theWindow->nextWindow;
- return( (WindowPtr)theWindow );
- }
-
- /*******************************************************
- * *
- * These take the basic actions dictated by events and mouse actions. SizeWindow *
- * should redraw any scrollbars, reallocate buffers, and invalidate regions. *
- * Content should handle mouse actions, including drags and double clicks. Update *
- * should handle any/all redrawing of window contents, including the scrollbars and *
- * growBox, and preferably do some of its own clipping rather than depending on *
- * the clipRgn to do everything for it. ChangeTo should at the very least select the *
- * window and set the port, and change any 'current' values. *
- * *
- *******************************************************/
-
- doSizeWindow( theWindow, w, h )
- WindowPtr theWindow;
- int w,h;
- {
- SizeWindow( theWindow, w, h, -1 );
- ClipRect( &( theWindow->portRect ));
- sizeData( GetWRefCon( theWindow ));
- }
-
- doShift( theWindow )
- WindowPtr theWindow;
- {
- shiftData( GetWRefCon( theWindow ));
- theWindow = frontUserWindow();
- if( theWindow ) SetPort( theWindow );
- }
-
- doContent( theWindow, theEvent )
- WindowPtr theWindow;
- EventRecord *theEvent;
- {
- dataHandle theDataHand;
- dataPtr theData;
- Point where;
- int whichCurs;
-
- theData = *( dataHandle )GetWRefCon( theWindow );
- where.h = theEvent->where.h + theWindow->portBits.bounds.left;
- where.v = theEvent->where.v + theWindow->portBits.bounds.top;
- whichCurs = getToolCurs();
- if( PtInRect( where, &( theData->dataRect )))
- if( theEvent->modifiers & cmdKey ) {
- forceCurs( marqeeCurs );
- doSelect( theWindow, theEvent );
- } else if( theData->isSelect && PtInRgn( where, theData->selRgn ) &&
- !( EqualRect( &( theData->dataRect ), &( theData->selBits.bounds ))
- && whichCurs == marqeeCurs )) {
- forceCurs( arrowCurs );
- doSelDrag( theWindow, theEvent );
- } else {
- forceCurs( whichCurs );
- switch( whichCurs ) {
- case pencilCurs:
- doBoxBrush( theWindow, theEvent, 1, 0 );
- break;
- case erasorCurs:
- doBoxBrush( theWindow, theEvent, 16, 1 );
- break;
- case elipseCurs:
- case lineCurs:
- case rectCurs:
- doBoxedShape( theWindow, theEvent, whichCurs );
- break;
- case marqeeCurs:
- doSelect( theWindow, theEvent );
- break;
- default: ;
- }
- }
- }
-
- doUpdate( theWindow )
- WindowPeek theWindow;
- {
- dataHandle theDataHand;
- dataPtr theData;
- GrafPtr savePort;
- Rect *bounds;
-
- GetPort( &savePort );
- SetPort( theWindow );
- theDataHand = ( dataHandle )GetWRefCon( theWindow );
- HLock( theDataHand );
- theData = *theDataHand;
- fixAllBits( theData );
- BeginUpdate( theWindow );
- bounds = &( thePort->portRect );
- CopyBig( &( theData->allBits ), &( thePort->portBits ), bounds, bounds, srcCopy, NULL );
- EndUpdate( theWindow );
- HUnlock( theDataHand );
- SetPort( savePort );
- }
-
- doChangeTo( theWindow )
- WindowPtr theWindow;
- {
- SelectWindow( theWindow );
- theWindow = frontUserWindow();
- if( theWindow ) SetPort( theWindow );
- }
-
- /*******************************************************
- * *
- * *
- *******************************************************/
-
- doSuspend( theEvent )
- EventRecord *theEvent;
- {
- WindowPeek theWindow;
-
- inBack = -1;
- theWindow = (WindowPeek)FrontWindow();
- if( theWindow )
- if( theWindow->windowKind >= userKind ) {
- exportScrap();
- doDeactivate( theWindow );
- } else {
- theEvent->what = activateEvt;
- theEvent->message = (long)theWindow;
- theEvent->modifiers &= deactiveMask;
- SystemEvent( theEvent );
- }
- else exportScrap();
- }
-
- doResume( theEvent )
- EventRecord *theEvent;
- {
- WindowPeek theWindow;
- int doClip;
-
- doClip = theEvent->message & clipFlag;
- inBack = 0;
- theWindow = (WindowPeek)FrontWindow();
- if( theWindow )
- if( theWindow->windowKind >= userKind ) {
- setBadScrap();
- doActivate( theWindow );
- } else {
- theEvent->what = activateEvt;
- theEvent->message = (long)theWindow;
- theEvent->modifiers |= activeFlag;
- SystemEvent( theEvent );
- }
- else setBadScrap();
- forceCurs( badCursFlag );
- }
-
- activeAnts( theDataHand, newValue )
- dataHandle theDataHand;
- int newValue;
- {
- GrafPtr savePort;
- Rect *bounds;
- dataPtr theData;
-
- HLock( theDataHand );
- theData = *theDataHand;
- if( theData->isSelect )
- {
- if( theData->selActive && !newValue ) --isBackground;
- else if( !( theData->selActive ) && newValue ) ++isBackground;
- theData->selActive = newValue;
- theData->allBitsState |= bitsBadAnts;
- GetPort( &savePort );
- SetPort( theData->parent );
- fixAllBits( theData );
- bounds = &( theData->selBits.bounds );
- CopyBig( &( theData->allBits ), &( thePort->portBits ), bounds, bounds, srcCopy, NULL );
- SetPort( savePort );
- }
- }
-
- doActivateEvt( theWindow )
- WindowPeek theWindow;
- {
- WindowPeek next;
-
- next = theWindow->nextWindow;
- if( next && next->windowKind < userKind )
- setBadScrap();
- doActivate( theWindow );
- }
-
- doActivate( theWindow )
- WindowPtr theWindow;
- {
- SetPort( theWindow );
- activeAnts( GetWRefCon( theWindow ), -1 );
- }
-
- doDeactivateEvt( theWindow )
- WindowPeek theWindow;
- {
- WindowPeek front;
-
- front = ( WindowPeek )FrontWindow();
- if( front->windowKind < userKind )
- exportScrap();
- doDeactivate( theWindow );
- }
-
- doDeactivate( theWindow )
- WindowPtr theWindow;
- {
- activeAnts( GetWRefCon( theWindow ), 0 );
- }
-
- /*******************************************************
- * *
- * *
- *******************************************************/
-
- makeDataWindow( theWindow, theFont, flags )
- WindowPtr theWindow;
- dataFontInfo *theFont;
- int flags;
- {
- ClipRect( &( theWindow->portRect ));
- TextFont( theFont->number );
- TextSize( theFont->size );
- TextFace( 0 );
- TextMode( srcOr );
- PenMode( patXor );
- newData( theWindow, theFont, flags );
- }
-
- doNew( flags )
- int flags;
- {
- dataFontInfo theFont;
- WindowPtr theWindow;
- char title[ pasStrLen ];
- Rect bounds;
-
- getDataFontInfo( &theFont, statFont );
- newCorner( &( bounds.left ), &( bounds.top ));
- bounds.right = defaultDataWidth + bounds.left;
- bounds.bottom = defaultDataHeight + bounds.top;
- if( flags & dataStatLine ) bounds.bottom += theFont.topSpace;
- GetIndString( title, windTitles, newWindTitle );
- theWindow = NewWindow( NULL, &bounds, title, -1, documentProc, (long)-1, -1, NULL );
- SetPort( theWindow );
- makeDataWindow( theWindow, &theFont, flags );
- }
-
- doFake( theWindow, flags )
- WindowPtr theWindow;
- int flags;
- {
- GrafPtr savePort;
- dataFontInfo theFont;
-
- GetPort( &savePort );
- SetPort( theWindow );
- getDataFontInfo( &theFont, statFont );
- makeDataWindow( theWindow, &theFont, flags );
- SetPort( savePort );
- }
-
- doClose( theWindow )
- WindowPeek theWindow;
- {
- int kind;
- WindowPeek next;
-
- kind = theWindow->windowKind;
- next = theWindow->nextWindow;
- if( kind >= userKind )
- {
- disposeData( GetWRefCon( theWindow ));
- if( next && next->windowKind < userKind )
- exportScrap();
- DisposeWindow( theWindow );
- }
- else {
- if( !next || ( next && next->windowKind >= userKind ))
- setBadScrap(); /* This may not be needed if the DeskAcc decides not to close... */
- installModalTrap();
- CloseDeskAcc( kind );
- removeModalTrap();
- }
- theWindow = (WindowPeek)frontUserWindow();
- if( theWindow ) SetPort( theWindow );
- }
-
- doAllClose()
- {
- WindowPeek theWindow, next;
-
- theWindow = (WindowPeek)FrontWindow();
- while( theWindow )
- {
- next = theWindow->nextWindow;
- doClose( theWindow );
- theWindow = next;
- }
- }